// DlgAddLayerTemplate.cpp : implementation file
//

#include "stdafx.h"
#include "ModelLayer.h"
#include "DlgAddLayerTemplate.h"
#include "afxdialogex.h"
#include "CHoopsView.h"


// CDlgAddLayerTemplate dialog

IMPLEMENT_DYNAMIC(CDlgAddLayerTemplate, CDialogEx)

CDlgAddLayerTemplate::CDlgAddLayerTemplate(CHoopsView* view)
	: CDialogEx(CDlgAddLayerTemplate::IDD, (CWnd*)view)
{
	m_pView = view;
	m_strTemplate=_T("");
	m_nRefLayerId = 0;

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

CDlgAddLayerTemplate::~CDlgAddLayerTemplate()
{
}

void CDlgAddLayerTemplate::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TEMPLATECOMBO, m_cbTemplate);
	DDX_Control(pDX, IDC_COMBO_REF_LAYER, m_cbRefLayer);
}


BEGIN_MESSAGE_MAP(CDlgAddLayerTemplate, CDialogEx)
	ON_BN_CLICKED(IDOK, &CDlgAddLayerTemplate::OnBnClickedOk)
	ON_CBN_SELCHANGE(IDC_COMBO_REF_LAYER, &CDlgAddLayerTemplate::OnSelchangeComboRefLayer)
END_MESSAGE_MAP()


// CDlgAddLayerTemplate message handlers


void CDlgAddLayerTemplate::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	int nCurSel=m_cbTemplate.GetCurSel();
	if(nCurSel==-1)
		m_strTemplate=_T("");
	else
		m_cbTemplate.GetLBText(nCurSel, m_strTemplate);

	int nIndex = m_cbRefLayer.GetCurSel();
	map<int , CLayer* >::iterator it_find = m_mapLayerRef.find(nIndex);
	if (it_find!=m_mapLayerRef.end())
	{
		CLayer *pRefLayer = it_find->second;
		m_nRefLayerId = pRefLayer->GetId();
	}

	CDialogEx::OnOK();
}


BOOL CDlgAddLayerTemplate::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);

	CString strPath=GetDllPath();
	int nIndex=strPath.ReverseFind('\\');
	if(nIndex!=-1)
	{
		strPath=strPath.Left(nIndex);
		std::list<CString> FileNameList;
		CFileFind ff;
		BOOL bFind=ff.FindFile(strPath+_T("\\template\\*.ini"));
		while(bFind)
		{
			bFind=ff.FindNextFile();

			CString strFile=ff.GetFileName();
			FileNameList.push_back(strFile);
		}
		ff.Close();

		std::list<CString>::iterator iter;
		for(iter=FileNameList.begin(); iter!=FileNameList.end(); ++iter)
			m_cbTemplate.AddString(*iter);
	}
	m_cbTemplate.SetCurSel(0);

	HModelLayerModel *pHmodel = (HModelLayerModel *)m_pView->GetHoopsView()->GetModel();
	if (!pHmodel)
	{
		return FALSE;
	}

	CSystem *pSystem = pHmodel->GetBigDrawing();
	pSystem->SortLayer();
	list<CLayer *> LayerList = pSystem->GetLayerList();
	int index=0;
	for (list<CLayer *>::iterator it_lay=LayerList.begin(); 
		it_lay!=LayerList.end(); it_lay++)
	{
		CLayer *pTempLayer = *it_lay;
		
		if (pTempLayer != pSystem->GetTopLayer())
		{
			CString szName = pTempLayer->GetName();
			m_cbRefLayer.AddString(szName);
			m_mapLayerRef.insert(make_pair(index, pTempLayer));
			index++;
		}
	}
	m_cbRefLayer.SetCurSel(m_mapLayerRef.size()-1);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


CString CDlgAddLayerTemplate::GetDllPath()
{
	TCHAR cPath[MAX_PATH];
	::GetModuleFileName(theApp.m_hInstance, cPath, MAX_PATH);

	CString strPath(cPath);
	int nIndex=strPath.ReverseFind('\\');
	if(nIndex!=-1)
		strPath=strPath.Left(nIndex);

	return strPath;
}

void CDlgAddLayerTemplate::OnSelchangeComboRefLayer()
{
	//m_nRefLayer = m_cbRefLayer.GetCurSel();
}
